<template>
    <div>
        <rui-page :defines="defines" v-show="!isShowConfig">
            <rui-query :label-width="150"
                       ref="searchFormPlan"
                       :query="queryData"
                       :query-rows="searchFormOptions"
            >
            </rui-query>
            <Row>
                <Alert show-icon>
                    已选择 <span class="select-count">{{selectCount}}</span> 项
                    <a class="select-clear" @click="clearSelectAll">清空</a>
                </Alert>
            </Row>
            <rui-table
                :defineId="'defineExtrasProgram'"
                :columns="['action','planNo','extrasPlanName','status','extrasProjectName']"
                :fixedLeft="['action']"
                :showIndex="false"
                :select="true"
                :slots="[{key:'action',slot:'action'},{key:'status',slot:'status'},{key:'extrasPlanName',slot:'extrasPlanName'}]"
                @loadDatas="queryData"
                ref-query="searchFormPlan"
                @on-selection-change="changeSelect"
                ref="extrasProgramTable"
            >
                <template slot="toolBar">
                    <Button @click="add" icon="md-add" type="primary">新增</Button>
                    <Button @click="effectiveAll" type="primary">有效</Button>
                    <Button @click="invalidAll" type="primary">无效</Button>
                </template>
                <template slot="action" slot-scope="{ row }">
                    <div>
                        <Button type="primary" size="small" v-if="row.status=='0'" @click="edit(row)">编辑</Button>
                        <Button type="error" size="small" v-if="row.status=='0'" @click="remove(row)">删除</Button>
                        <Button @click="view(row)" size="small" type="primary" v-if="row.status=='1'">查看</Button>
                        <Button @click="copy(row)" size="small" v-if="row.status=='1'" type="primary">复制</Button>
                    </div>
                </template>
                <template slot-scope="{ row, index }" slot="status">
                    <div>
                        <tag v-if="row.status=='1'" color="success" size="default">有效</tag>
                        <tag v-if="row.status=='0'" color="error" size="default">无效</tag>
                    </div>
                </template>
                <template slot-scope="{ row, index }" slot="extrasPlanName">
                    <a @click="view(row)">
                        {{row.extrasPlanName}}
                    </a>
                </template>
            </rui-table>

            <Modal
                v-model="isShowCopy"
                :title="copyTitle"
                :width="500"
                :closable="false"
                :mask-closable="false"
                @keydown.enter.native="saveCopy"
                @on-ok="saveCopy">
                <rui-form :form-options="formOptions"
                          :label-width="150"
                          :read-only="false"
                          ref="showForm">
                </rui-form>
                <div slot="footer">
                    <Button size="small" @click="isShowCopy = false">取消</Button>
                    <Button type="primary" size="small" @click="saveCopy">保存</Button>
                </div>
            </Modal>

        </rui-page>
        <extrasProgramConfig @next="parentReceive" v-if="isShowConfig" :op="op" :planId="planId" :fitScope="fitScope"
                             :schemeForm="schemeForm" @backInfo="backInfo"/>
    </div>
</template>
<script>
    import defineExtrasProgram from "./define/extras-program-define";
    import extrasProgramConfig from "./extrasProgramConfig.vue";
    import {deepClone} from "@/libs/utils/ObjectClone";
    import {
        deleteById,
        getExtrasProgramListData,
        getSelectProjectNames,
        effectiveExtrasProgram,
        invalidExtrasProgram,
        copyPlan,
    } from "@/projects/afs-core-business/api/afs-product/product/extrasProgram";

    export default {
        name: "extras-program",
        components: {
            extrasProgramConfig
        },
        data() {
            return {
                isShowConfig: false,
                modalTitle: "附加贷方案清单",
                locationData: [],
                searchFormOptions: [
                    {defineId: "defineExtrasProgram", fields: ["planNo", "extrasPlanName", "status"],},
                ],
                selectCount: 0,
                isShowCopy: false,
                copyTitle: "复制方案",
                formOptions: [
                    {
                        isGroup: false,
                        grids: [{defineId: "defineExtrasProgram", span: 24, fields: ["extrasPlanName"]},]
                    }
                ],
                submitLoading: false,

                //规则配置页面
                op: '',
                planId: '',
                fitScope: '003',
                schemeForm: {},
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "defineExtrasProgram",
                        fields: defineExtrasProgram
                    }
                ];
            }
        },
        methods: {
            selectextrasProjectNames() {
                getSelectProjectNames().then(res => {
                    if (res.code === "0000") {
                        this.locationData = res.data;
                    }
                });
            },
            queryData(queryData) {
                this.submitLoading = true;
                getExtrasProgramListData(queryData).then(res => {
                    this.submitLoading = false;
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.extrasProgramTable.updateTableData(records, total);
                    }
                });
            },
            clearSelectAll() {
                this.$refs.extrasProgramTable.getTable().selectAll(false);
            },
            refresh() {
                this.$refs['extrasProgramTable'].reloadData();
                this.clearSelectAll();
            },
            changeSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            effectiveAll() {
                if (this.selectCount <= 0 || this.selectCount == undefined) {
                    this.$Message.warning("您还未选择数据");
                    return;
                }
                if (this.check("effective")) {
                    return;
                }
                this.$Modal.confirm({
                    title: "确认生效",
                    onOk: () => {
                        let ids = [];
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        effectiveExtrasProgram(ids).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success("操作成功");
                                this.refresh();
                            }
                        });
                    }
                });
            },
            invalidAll() {
                if (this.selectCount <= 0 || this.selectCount == undefined) {
                    this.$Message.warning("您还未选择数据");
                    return;
                }
                if (this.check("invalid")) {
                    return;
                }
                this.$Modal.confirm({
                    title: "确认无效",
                    onOk: () => {
                        let ids = [];
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        invalidExtrasProgram(ids).then(res => {
                            if (res.code === "0000") {
                                this.refresh();
                                this.$Message.success("设置无效成功");
                            }
                        });
                    }
                });
            },
            check(type) {
                console.info("校验"+type);
                let invalidNum = 0;
                let effectiveNum = 0;
                this.selectList.forEach((e) => {
                    if (e.status == 0) {//无效
                        invalidNum++;
                    } else if (e.status == 1) {//有效
                        effectiveNum++;
                    }
                });
                console.info("有效:"+effectiveNum,"无效"+invalidNum);
                var checkResult = false;
                if ("invalid" == type && invalidNum > 0) {
                    this.$Message.warning("不可设置无效数据为无效！");
                    checkResult = true;
                }
                if ("effective" == type && effectiveNum > 0) {
                    this.$Message.warning("不可设置有效数据为有效！");
                    checkResult = true;
                }
                return checkResult;
            },
            add() {
                this.isShowConfig = true;
                this.op = 'add';
                this.planId = null;
                this.schemeForm = {
                    planNo: null,
                    name: null,
                };
            },
            edit(row) {
                this.isShowConfig = true;
                this.op = 'edit';
                this.planId = row.id;
                this.schemeForm = row;
            },
            view(row) {
                this.isShowConfig = true;
                this.isShow = false;
                this.op = 'view';
                this.planId = row.id;
                this.schemeForm = row;
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除吗",
                    content: "您确认要删除这条数据?",
                    onOk: () => {
                        deleteById(v.id).then(res => {
                            if (res.code === "0000") {
                                this.refresh();
                            }
                            this.$Message.success(res.msg);
                        });
                    }
                });
            },
            parentReceive() {
                this.isShowConfig = false;
                this.refresh();
            },
            copy(row) {
                this.isShowCopy = true;
                this.$refs.showForm.resetFrom();
                this.$refs.showForm.setFieldRequired('extrasPlanName', true);
                this.$refs.showForm.updateFormData(deepClone(row));
            },
            saveCopy() {
                this.submitLoading = true;
                this.$refs.showForm.getForm().validate((valid) => {
                    if (valid) {
                        copyPlan(this.$refs.showForm.getFormData()).then(res => {
                            this.submitLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("复制成功");
                                this.isShowCopy = false;
                                this.refresh();
                            }
                        }).catch(() => {
                            this.submitLoading = false;
                        });
                    }
                });
            },
            backInfo(val) {
                console.info("回调值：" + JSON.stringify(val));
                this.op = 'edit';
                this.planId = val.id;
                this.schemeForm = val;
            },
        },
        mounted() {
            this.refresh();
        }
    };
</script>
